package com.scs.application.modules.dept.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.DateUtils;
import com.scs.application.modules.base.entity.Dept;
import com.scs.application.modules.base.service.DeptService;
import com.scs.application.modules.dept.entity.Budget;
import com.scs.application.modules.dept.entity.BudgetItem;
import com.scs.application.modules.dept.entity.UseStats;
import com.scs.application.modules.dept.mapper.UseStatsMapper;
import com.scs.application.modules.dept.service.BudgetItemService;
import com.scs.application.modules.dept.service.BudgetService;
import com.scs.application.modules.dept.service.UseStatsService;
import com.scs.application.modules.finance.entity.Bill;
import com.scs.application.modules.finance.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Service
@Deprecated
public class UseStatsServiceImpl extends BaseServiceImpl<UseStatsMapper, UseStats> implements UseStatsService {

    @Autowired
    private DeptService deptService;

    @Autowired
    private BudgetService budgetService;

    @Autowired
    private BudgetItemService budgetItemService;

    @Autowired
    private BillService billService;

    /**
     * 自动科室消耗统计
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void statDeptUse() {
        Budget budget = budgetService.getCurrentBudget();

        if (budget == null) {
            return;
        }

        List<Dept> deptList = deptService.list();
        deptList.forEach(dept -> {
            UseStats useStats = super.getOne(new QueryWrapper<UseStats>()
                    .eq("period_year", budget.getPeriodYear())
                    .eq("dept_id", dept.getId()));
            if (useStats == null) { //没有统计信息，先创建一个
                useStats = new UseStats();
                BudgetItem budgetItem = budgetItemService.getOne(new QueryWrapper<BudgetItem>()
                        .eq("period_year", budget.getPeriodYear())
                        .eq("dept_id", dept.getId()));
                if (budgetItem != null) {
//                    useStats.setBudgetAmount(budgetItem.getBudgetAmount());
                }
                useStats.setDeptId(dept.getId())
                        .setDeptName(dept.getName())
                        .setPeriodYear(budget.getPeriodYear())
                        .setBudgetStartDate(budget.getStartDate());
            }

            //统计年、月、周消耗
            Double amountYear = billService.list(new QueryWrapper<Bill>()
                    .eq("dept_id", dept.getId())
                    .eq("flag_dept", 1)
                    .gt("bus_date", budget.getStartDate()))
                    .stream().mapToDouble(Bill::getAmount).sum();


            Double amountMonth = amountYear;
            Date firstDayOfMonth = DateUtils.getFirstDayOfMonth(Calendar.getInstance().getTime());
            if(firstDayOfMonth.after(budget.getStartDate())) { //如果当月小于当年日期，则取当年日期
                amountMonth = billService.list(new QueryWrapper<Bill>()
                        .eq("dept_id", dept.getId())
                        .eq("flag_dept", 1)
                        .gt("bus_date", firstDayOfMonth))
                        .stream().mapToDouble(Bill::getAmount).sum();
            }

            Double amountWeek = amountYear;
            Date firstDayOfWeek = DateUtils.getFirstDayOfWeek(Calendar.getInstance().getTime());
            if(firstDayOfMonth.after(budget.getStartDate())) { //如果当周小于当年日期，则取当年日期
                amountWeek = billService.list(new QueryWrapper<Bill>()
                        .eq("dept_id", dept.getId())
                        .eq("flag_dept", 1)
                        .gt("bus_date", firstDayOfWeek))
                        .stream().mapToDouble(Bill::getAmount).sum();
            }
            useStats.setAmountMonth(amountMonth)
                    .setAmountWeek(amountWeek)
                    .setAmountYear(amountYear);
            super.saveOrUpdate(useStats);

            //更新科室计划实际消耗
            BudgetItem budgetItem = budgetItemService.getOne(new QueryWrapper<BudgetItem>()
                    .eq("period_year", budget.getPeriodYear())
                    .eq("dept_id", dept.getId()));
            if (budgetItem != null) {
//                budgetItem.setUseAmount(useStats.getAmountYear());
                budgetItemService.updateCascadeById(budgetItem);
            }
        });
    }
}
